package com.zhx.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhx.dao.RoleDao;
import com.zhx.dao.RoleMenuDao;
import com.zhx.menu.RoleMenu;
import com.zhx.role.Role;
/**
 * 角色管理控制模块
 * @author 演示
 *
 */
public class RoleServlet extends BaseServlet {
		
		@Override
		protected void doGet(HttpServletRequest req, HttpServletResponse resp)
				throws ServletException, IOException {
			this.doPost(req, resp);
		}

		@Override
		protected void doPost(HttpServletRequest req, HttpServletResponse resp)
				throws ServletException, IOException {
			String m =req.getParameter("m");
			if("list".equals(m)){
				list(req, resp);
			}else if("add".equals(m)){
				add(req, resp);
			}else if("edit".equals(m)){
				edit(req, resp);
			}else if("openAdd".equals(m)){
				process(req, resp, "/page/role/role_add.jsp");
			}else if("openEdit".equals(m)){
				process(req, resp, "/page/role/role_edit.jsp");
			}else if("del".equals(m)){
				del(req, resp);
			}
		}
		
		/**
		 * 列表
		 * @param req
		 * @param resp
		 * @throws ServletException
		 * @throws IOException
		 */
		private void list(HttpServletRequest req, HttpServletResponse resp)
				throws ServletException, IOException{
			  process(req, resp, "/page/role/role_list.jsp");
		}
		
		/**
		 * 编辑
		 * @param req
		 * @param resp
		 * @throws ServletException
		 * @throws IOException
		 */
		private void edit(HttpServletRequest req, HttpServletResponse resp)
				throws ServletException, IOException{
			try {
				// 获取用户参数
				Role role = new Role();
				String id = req.getParameter("id");
				if(id != null) {
					role.setId(Integer.valueOf(id));
					role.setRoleId(req.getParameter("roleId"));
					role.setRoleName(req.getParameter("roleName"));
					role.setUpdateTime(new Timestamp(System.currentTimeMillis()));
					
					// 修改信息
					RoleDao roleDao = new RoleDao();
					roleDao.updateRole(role);
					
					process(req, resp, "/page/role/role_list.jsp");
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		/**
		 * 添加
		 * @param req
		 * @param resp
		 * @throws ServletException
		 * @throws IOException
		 */
		private void add(HttpServletRequest req, HttpServletResponse resp)
				throws ServletException, IOException{
			// 获取用户参数
			Role role = new Role();
			role.setRoleId(req.getParameter("roleId"));
			role.setRoleName(req.getParameter("roleName"));
			role.setCreateTime(new Timestamp(System.currentTimeMillis()));
			role.setUpdateTime(new Timestamp(System.currentTimeMillis()));
			String[] menus = req.getParameterValues("menuIds");
			
			// 添加到数据库
			RoleDao roleDao = new RoleDao();
			RoleMenuDao roleMenuDao = new RoleMenuDao();
			try {
				roleDao.add(role);
				roleMenuDao.addRoleMenus(role.getRoleId(), menus);
				process(req, resp, "/page/role/role_list.jsp");
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		/**
		 * 删除
		 * @param req
		 * @param resp
		 * @throws ServletException
		 * @throws IOException
		 */
		private void del(HttpServletRequest req, HttpServletResponse resp)
				throws ServletException, IOException{
			try {
				// 获取用户参数
				Role role = new Role();
				String id = req.getParameter("id");
				if(id != null) {
					role.setId(Integer.valueOf(id));
										
					// 修改信息
					RoleDao roleDao = new RoleDao();
					roleDao.delete(role);
					
					process(req, resp, "/page/role/role_list.jsp");
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
    
}
